/*
* UserController.java
* Created on  2013-9-26 下午10:42
* 版本       修改时间          作者      修改内容
* V1.0.1    2013-9-26       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.admin.web;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.scorm.service.ValidateImageService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.GeneralSecurityException;

/**
 * @author ljb
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/admin/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ValidateImageService validateImageService;

    @RequestMapping(value = "showImage")
    @ResponseBody
    public void showImage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        validateImageService.validateImage(request,response);
    }

    @RequestMapping(value = "loginDo", method = {RequestMethod.GET})
    public String login(HttpServletRequest request) {
        return "login";
    }



    @RequestMapping(value = "listAllUserDo", method = {RequestMethod.GET})
    public String listAllUserDo() {
        return "admin/user/listAllUserDo";
    }

    @RequestMapping(value = "listAllUser", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllUser(PageParameter pageParameter, User user) {
        return userService.findConsumerPageList(pageParameter, user);
    }

    @RequestMapping(value = "addUserDo", method = {RequestMethod.GET})
    public String addUserDo() {
        return "admin/user/addUserDo";
    }

    @RequestMapping(value = "addUser", method = {RequestMethod.POST})
    @ResponseBody
    public void addUser(User user) {
        userService.addUser(user);
    }

    @RequestMapping(value = "addAdministrator", method = {RequestMethod.POST})
    @ResponseBody
    public void addAdministrator(User user) {
        userService.addAdministrator(user);
    }

    @RequestMapping(value = "checkRepeatLoginName", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkRepeatPermissionToken(@RequestParam("loginName") String loginName,@RequestParam("oldName") String oldName) {
        if (loginName!=oldName){
        return userService.checkRepeatLoginName(loginName, oldName);
        }else{
            return false;
        }
    }


    @RequestMapping(value = "checkRepeatUserName", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkRepeatUserName(@RequestParam("userName") String userName,@RequestParam("oldName") String oldName) {
        return userService.checkRepeatUserName(userName, oldName);
    }
    @RequestMapping(value = "checkRepeatImageCode", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkRepeatImageCode(@RequestParam("imageCode") String imageCode) {
        return validateImageService.checkImageCode(imageCode);
    }

    @RequestMapping(value = "editUserDo", method = {RequestMethod.GET})
    public String editUserDo(HttpServletRequest request, @RequestParam("id") int id) {
        User user = userService.findUserAllInfoById(id);
        request.setAttribute("user", user);
        return "admin/user/editUserDo";
    }

    @RequestMapping(value = "editAdministratorDo", method = {RequestMethod.GET})
    public String editAdministratorDo(HttpServletRequest request, @RequestParam("id") int id) {
        User user = userService.findUserAllInfoById(id);
        request.setAttribute("user", user);
        return "admin/user/editAdministratorDo";
    }

    @RequestMapping(value = "changePasswordDo", method = {RequestMethod.GET})
        public String changePasswordDo() {
            return "admin/user/changePasswordDo";
        }

    @RequestMapping(value = "changePassword", method = {RequestMethod.POST})
    @ResponseBody
    public void changePassword(@RequestParam("newPassword") String newPassword) throws GeneralSecurityException {
        User user = userService.findUserByLoginName(LoginUserUtil.findLoginUserName());
        userService.changeUserPassword(user, newPassword);
    }

    @RequestMapping(value = "editUser", method = {RequestMethod.POST})
    @ResponseBody
    public void editUser(User user) {
        userService.editUser(user);
    }

    @RequestMapping(value = "editAdministrator", method = {RequestMethod.POST})
    @ResponseBody
    public void editAdministrator(User user) {
        userService.editAdministrator(user);
    }

    @RequestMapping(value = "delUser", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delUser(@RequestParam("ids") String ids) throws GeneralSecurityException {
        String[] userIds = ids.split(",");
        userService.delUsers(userIds);
    }

    @RequestMapping(value = "delAdministrator", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delAdministrator(@RequestParam("ids") String ids) throws GeneralSecurityException {
        String[] userIds = ids.split(",");
        userService.delAdministrator(userIds);
    }

    @RequestMapping(value = "listAllAdministratorDo", method = {RequestMethod.GET})
        public String listAllAdministratorDo() {
            return "admin/user/listAllAdministratorDo";
       }

    @RequestMapping(value = "listAllAdministrator", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllAdministrator(PageParameter pageParameter, User user) {
        return userService.findAdministratorPageList(pageParameter, user);
    }

    @RequestMapping(value = "addAdministratorDo", method = {RequestMethod.GET})
        public String addAdministratorDo() {
            return "admin/user/addAdministratorDo";
        }

    @RequestMapping(value = "baseInfoDo", method = {RequestMethod.POST,RequestMethod.GET})
    public String baseInfoDo(HttpServletRequest request){
        HttpSession session = request.getSession();
        int id= Integer.parseInt(session.getAttribute("userId").toString()) ;
        User user= userService.findUserAllInfoById(id);
        request.setAttribute("user",user);
        return "consumer/baseInfo";
    }

    @RequestMapping(value = "checkPassword", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkPassword(@RequestParam("oldPassword") String oldPassword) {
        return userService.checkPassword(oldPassword);
    }
}
